package com.atguigu.web;

import com.atguigu.pojo.Food;
import com.atguigu.pojo.Cart;
import com.atguigu.pojo.CartItem;
import com.atguigu.service.FoodService;
import com.atguigu.service.impl.FoodServiceImpl;
import com.atguigu.utils.WebUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CartServlet extends BaseServlet {
    private static int cartId = 1;
    private CartItem cartItems = new CartItem();
    private FoodService foodService = new FoodServiceImpl();
    /**
     * 添加商品  跳回主页面
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//         1、采用请求转发来响应加入菜品车事件
        req.setCharacterEncoding("utf-8");
        // 1、得到加入的菜品id，将菜品封装到Cart对象中
        Food food = foodService.queryFoodById(WebUtils.parseInt(req.getParameter("id")));
        String foodName = req.getParameter("foodName");
        Cart cart = new Cart(food.getId(), food.getFood_name(), 1, new BigDecimal(food.getPrice()), new BigDecimal(food.getPrice()));
        // 2、将cart对象写入到CartItem对象中
        cartItems.addItem(cart);
        // 3、将CartItem对象写入Session域中
        req.getSession().setAttribute("cartItems", cartItems);
        req.getSession().setAttribute("lastName", foodName);
        // 4、请求转发至 /pages/client/index.jsp
        resp.sendRedirect(req.getHeader("Referer"));
    }

    /**
     * 删除商品
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        cartItems.deleteItem(WebUtils.parseInt(req.getParameter("id")));
        resp.sendRedirect(req.getHeader("Referer"));
    }

    /**
     * 修改商品数量
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void modifyItemCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1、接收客户端传过来的修改数量，和商品id
        String count = req.getParameter("count");
        String id = req.getParameter("id");
        // 2、修改商品数量
        cartItems.updateCount(WebUtils.parseInt(id), WebUtils.parseInt(count));
        // 3、请求重定向去原地址
        resp.sendRedirect(req.getHeader("Referer"));
    }

    /**
     * 清空菜品车
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void removeAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        cartItems.clear();
        resp.sendRedirect(req.getHeader("Referer"));
    }

    /**
     * 利用ajax请求来完成加入菜品车操作
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void ajaxToCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 2、采用ajax请求来响应加入菜品车事件
        req.setCharacterEncoding("utf-8");
        // 1、获得菜品id
        String id = req.getParameter("id");
        String foodName = req.getParameter("foodName");
        Food food = foodService.queryFoodById(WebUtils.parseInt(id));
        // 将得到的菜品信息封装到菜品车对象中
        Cart cart = new Cart(food.getId(), food.getFood_name(), 1, new BigDecimal(food.getPrice()), new BigDecimal(food.getPrice()));
        // 将菜品车对象封装到菜品车总信息对象中
        cartItems.addItem(cart);
        // 将cartItems对象封装到Map中, 封装最后一个加入菜品车的书籍
        Map<String, Object> map = new HashMap<>();
        map.put("totalCount", cartItems.getTotalCount());
        map.put("lastName", foodName);

        // 3、将CartItem对象写入Session域中
        req.getSession().setAttribute("cartItems", cartItems);
        req.getSession().setAttribute("lastName", foodName);

        // 将map对象转换为json对象  因为浏览器主要容易处理json对象
        Gson gson = new Gson();
        String s = gson.toJson(map);

        // 将json对象写入到客户端的data数据中
        resp.getWriter().write(s);
    }
}
